package com.shanghaionstar.smart;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.midea.iot.sdk.cloud.openapi.common.MSmartKeyDefine;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.shanghaionstar.R;
import com.shanghaionstar.smart.iview.ISmartSettingWifiView;
import com.shanghaionstar.smart.presenter.SmartWifiSettingPresenter;

/**
 * Created by Rengui Lu on 2016/6/12.
 */

//        ADD_DEVICE
//        添加设备.
//        CONNECT_DEVICE
//        连接设备.
//        CONNECT_DEVICE_AP
//        连接设备热点.
//        CONNECT_ROUTER
//        连接路由器.
//        ENABLE_WIFI
//        启用WIFI.
//        FIND_DEVICE_IN_AP
//        在设备热点网络中找设备.
//        FIND_DEVICE_IN_ROUTER
//        在路由器中找设备.
//        FIND_DEVICE_IN_WAN
//        在服务器中找设备.
//        GET_DEVICE_BASIC_INFO
//        获取设备基本信息.
//        RECONNECT_ROUTER
//        连接路由器.
//        SEND_MSC_BROADCAST
//        发送快连广播.
//        SWITCH_STA
//        切换至STA模式.
//        WRITE_DEVICE_ID
//        写设备ID.
//        WRITE_WIFI_CONFIGURATION
//        写WIFI配置参数.
public class SmartSettingWifiFrg extends BaseFragment implements View.OnClickListener, ISmartSettingWifiView {
    @FindViewById(R.id.title_tv)
    TextView mTitleTv;
    @FindViewById(R.id.ssid_tv)
    TextView mSsidTv;
    @FindViewById(R.id.pw_tv)
    TextView mPwTv;
    @FindViewById(R.id.sub_title_tv)
    TextView mSubTitleTv;
    @FindViewById(R.id.back_ib)
    ImageButton mBackIb;


    private BaseActivity mActivity;
    private SmartWifiSettingPresenter presenter;


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = (BaseActivity) activity;
    }

    @Override
    public int getLayoutId() {
        return R.layout.smart_home_setting_wifi;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        if (isFirstLoad()) {
            presenter = new SmartWifiSettingPresenter(mActivity.getScheduler());
            presenter.setView(this);
        }

    }

    private void initView() {
        mTitleTv.setText(R.string.smart_add_device);
        mSubTitleTv.setText(R.string.smart_fun_intro);
        Bundle bundle = getArguments();
        String deviceID = bundle.getString(MSmartKeyDefine.KEY_DEVICE_ID);
        String currStep = bundle.getString(MSmartKeyDefine.KEY_STEP_NAME);
        mSsidTv.setText(getString(R.string.smart_wifi_ssid)+deviceID);
        mPwTv.setText(getString(R.string.smart_wifi_pw)+currStep);
    }

    @Override
    public void onResume() {
        super.onResume();
    }



    private void goToAddDevice() {
        SmartAddDeviceFrg addFrg = new SmartAddDeviceFrg();
        mActivity.changeFragment(mActivity.getSupportFragmentManager(), addFrg, SmartAddDeviceFrg.class, true);
    }

    public void goToWifiSetting() {
        Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
        mActivity.startActivity(intent);
    }

    @OnClick({R.id.no_found_tv, R.id.connect_btn, R.id.connected_btn, R.id.sub_title_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.no_found_tv:
                presenter.goToNoFoundDevice();
                break;
            case R.id.sub_title_tv:
                presenter.goToFunIntro();
                break;
            case R.id.connect_btn:
                goToWifiSetting();
                break;
            case R.id.connected_btn:
                goToAddDevice();
                break;
        }
    }


    @Override
    public void setLoading(boolean isLoading) {

    }

    @Override
    public void onResponseError(OnStarException e) {

    }

    @Override
    public void onReponseSuccess(Object object) {

    }

    @Override
    public Activity onActivity() {
        return mActivity;
    }
}
